package com.lin.cloud.service;

import com.lin.cloud.feign.ConsumerFeign;
import com.lin.entity.JsonResult;
import com.lin.entity.Student;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.client.RestTemplate;

/**
 * @author longyang.lin
 * @description
 * @create 2019年01月08日19:42
 */
@Service
@Slf4j
public class ConsumerServiceImpl {
    /**
     * feign
     */
    @Autowired
    private ConsumerFeign consumerFeign;

    /**
     * ribbon
     */
    @Autowired
    private RestTemplate restTemplate;


    public String getName(@PathVariable String name) {
        String consumerName = this.consumerFeign.getName(name);
        return "consumer api:" + consumerName;
    }

    public String restTemplateGetName(@RequestParam String name) {
        return this.restTemplate.getForObject("http://EUREKA-PROVIDER/provider/getName/{name}", String.class, name);
    }

    @HystrixCommand(fallbackMethod = "fallback")
    public JsonResult<Student> insertStu() {
        JsonResult result = this.consumerFeign.insertStudent(new Student("test", 15));
        log.info("result==>{}", result);
        return result;
    }

    public JsonResult<Student> fallback() {
        return new JsonResult<>();
    }
}
